<?php
require '../utils/conn.php';
include '../models/Advertising.php';
include 'apns.php';  

function addAdvertising($advertising)
{
	$currentTime = time();
	$advertising->createTime = $currentTime;
	$query = "INSERT INTO advertising(title, summary,ad_url, details, create_time, status, category)" . " VALUES('$advertising->title','$advertising->summary','$advertising->adUrl','$advertising->details','$currentTime',$advertising->status,'$advertising->category')";

	$result = mysql_query($query) or die("添加广告出现错误：". mysql_error());
	$advertising->adId = mysql_insert_id();
	
	if ($advertising->status == 1) {
		$query = "UPDATE t_news_count SET news_count=(news_count+1)";
		$result = mysql_query($query);
		pushMessageToAllUser($advertising->title);
	}
	
	return $advertising;
}

function updateAdvertising($advertising)
{
	$currentTime = time();
	$advertising->createTime = $currentTime;
	$query = "UPDATE advertising SET title='$advertising->title', summary='$advertising->summary', ad_url='$advertising->adUrl', details='$advertising->details', create_time='$currentTime', status=$advertising->status, category='$advertising->category' WHERE ad_id=".$advertising->adId;

	mysql_query($query) or die("更新广告出现错误：". mysql_error());
	
	if ($advertising->status == 1) {
		$query = "UPDATE t_news_count SET news_count=(news_count+1)";
		$result = mysql_query($query);
		pushMessageToAllUser($advertising->title);
	}
}

function deleteAdvertising($adId)
{
	$query = "DELETE FROM advertising WHERE ad_id=".$adId;
	mysql_query($query);
}

function getUpdateAdvertisingCount($timestamp) {
	$query = "SELECT count(*) FROM advertising WHERE create_time>".$timestamp." and status=1";
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		return $row['count(*)'];
	}
	return 0;
}

function getAllAdvertising()
{
	$query = "SELECT * FROM advertising ORDER BY create_time DESC";
	$advertisings = array();
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$advertising = new Advertising();
		$advertising->setAdId($row['ad_id']);
		$advertising->setTitle($row['title']);
		$advertising->setSummary($row['summary']);
		$advertising->setAdUrl($row['ad_url']);
		$advertising->setDetails($row['details']);
		$advertising->setCreateTime($row['create_time']);
		$advertising->setStatus($row['status']);
		$advertising->setCategory($row['category']);
		$advertisings[] = $advertising;
	}
	return $advertisings;
}

function getActiveAdvertising()
{
	$query = "SELECT * FROM advertising WHERE status=1 ORDER BY create_time DESC";
	$advertisings = array();
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$advertising = new Advertising();
		$advertising->setAdId($row['ad_id']);
		$advertising->setTitle($row['title']);
		$advertising->setSummary($row['summary']);
		$advertising->setAdUrl($row['ad_url']);
		$advertising->setDetails($row['details']);
		$advertising->setCreateTime($row['create_time']);
		$advertising->setStatus($row['status']);
		$advertising->setCategory($row['category']);
		$advertisings[] = $advertising;
	}
	return $advertisings;
}

?>